<template>
  <div class="common-layout">
    <el-form class="header" :model="queryParams" ref="queryRef" :inline="true">
      <el-form-item label="记录日期" prop="date">
        <el-date-picker
            v-model="queryParams.date"
            value-format="YYYY-MM-DD"
            type="date"
            placeholder="请选择日期"
        ></el-date-picker>
      </el-form-item>

      <el-form-item label="组号" prop="group">
        <el-input clearable v-model="groupNum"
                  @blur="inputKeyUpEvent"
                  placeholder="请输入组号(1-50)"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="searchByUnit">筛选</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="20">
      <el-col :sm="24" :lg="16" style="padding-left: 20px;padding-right: 20px">
        <el-table border :data="wordTestList">
          <el-table-column label="测试编号" width="80" align="center" prop="id"/>
          <el-table-column label="组号" align="center" prop="organize">
            <template #default="scope">
              {{ scope.row.organize === null ? '暂无组号' : scope.row.organize.toLocaleString().substring(5) }}
            </template>
          </el-table-column>
          <el-table-column label="测试个数" width="80" align="center" prop="times"/>
          <el-table-column label="正确个数" align="center" prop="correctNum"/>
          <el-table-column label="完成个数" align="center" prop="complete"/>
          <el-table-column v-if="false" label="总数" align="center" prop="wordNum"/>
          <el-table-column label="记录时间" width="180" align="center" prop="createTime"/>
          <el-table-column label="完成率" align="center" prop="rate">
            <template #default="scope">
              <span>{{ parseInt(scope.row.complete * 100 / scope.row.wordNum).toFixed(0) }}%</span>
            </template>
          </el-table-column>
          <el-table-column label="正确率" align="center" prop="correctRate">
            <template #default="scope">
              <span>{{ parseInt(scope.row.correctRate * 100).toFixed(0) }}%</span>
            </template>
          </el-table-column>
          <el-table-column label="用时" align="center" prop="usedTime"/>
          <el-table-column label="详情" align="center" width="80" class-name="small-padding fixed-width">
            <template #default="scope">
              <el-button link @click="getTestDetails(scope.row)" type="primary">查看</el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination
            style="margin-right: 20px"
            v-show="size > 0"
            :total="size"
            v-model:page="queryParams.pageNum"
            v-model:limit="queryParams.pageSize"
            @pagination="handleRecordData"
        />
      </el-col>

    </el-row>

    <el-dialog style="padding: 20px" title="听力测试记录" v-model="testResultWindow" width="900px" append-to-body>
      <el-row type="flex" justify="center">
        <el-text style="font-size: 18px;color: rebeccapurple">
          测试总数:{{ testTotalNum }}---完成个数{{
            testCompleteNum
          }}---正确个数:{{ correctNum }}---完成率:{{ parseInt(testCompleteRate) }}%---正确率:{{ parseInt(testCorrectRate) }}%
        </el-text>
      </el-row>
      <el-table :data="testCompleteData">
        <el-table-column label="组号" align="center" prop="group">
          <template #default="scope">
            {{ scope.row.group === null ? '暂无组号' : scope.row.group.toLocaleString().substring(5) }}
          </template>
        </el-table-column>
        <el-table-column label="单词/词组" align="center" prop="name"/>
        <el-table-column label="音频播放次数" align="center" prop="heardTimes"/>
        <el-table-column label="我的拼写" align="center" prop="nameAgain"/>
        <el-table-column label="拼写用时(秒)" align="center" prop="wordTime"/>
        <el-table-column label="测试结果" width="100" align="center" prop="result">
          <template #default="scope">
            <dict-tag :options="word_test_status" :value="scope.row.result"/>
          </template>
        </el-table-column>
        <el-table-column label="完成时间" width="180" align="center" prop="time"/>
      </el-table>

      <pagination
          style="margin-right: 20px"
          v-show="false"
          :total="testTotal"
          v-model:page="queryTest.pageNum"
          v-model:limit="queryTest.pageSize"
          @pagination=""
      />
    </el-dialog>
  </div>
</template>

<script setup>
// 查询
import {listHearRecordDetail} from "@/api/word/index.js";
import {getCurrentInstance, ref} from 'vue';
import {listHearRecord} from "@/api/word/wordRecord.js";
import useUserStore from '@/store/modules/user'
import {isNumberStr} from "@/utils/index.js";

const {proxy} = getCurrentInstance();
const {word_test_status} = proxy.useDict("word_test_status")

// 弹窗
let correctNum = ref(0)
let testTotal = ref(0)
let testTotalNum = ref(0)
let testCompleteNum = ref(0)
let testCompleteData = ref([])
let testCompleteRate = ref(0)
let testCorrectRate = ref(0)
let testResultWindow = ref(false)
let groupNum = ref(null)
let queryTest = ref({
  pageNum: 1,
  pageSize: 10
})

let queryParams = ref({
  pageNum: 1,
  userId: useUserStore().id,
  pageSize: 10,
  date: '',
  organize: ''
})

let wordTestList = ref([])
let size = ref(0)

initPage()

//连接MYSQL数据库-查询全部
function handleRecordData() {
  // console.log(queryParams.value)
  if (groupNum.value !== undefined && groupNum.value !== '' && groupNum.value !== null) {
    queryParams.value.organize = "GROUP" + groupNum.value
  }
  listHearRecord(queryParams.value).then(res => {
    if (res.code === 200) {
      wordTestList.value = res.rows;
      size.value = res.total;
      // console.log(res)
    }
  })
}

function getTestDetails(row) {
  // console.log(row)
  if (row.practice === undefined || row.practice === null) {
    proxy.$modal.msgWarning("此记录详情未开放")
    return
  }
  testCorrectRate.value = parseInt(row.correctRate * 100)
  correctNum.value = row.correctNum
  testTotalNum.value = row.wordNum
  testCompleteNum.value = row.complete
  testCompleteRate.value = parseInt(row.complete) / parseInt(row.wordNum) * 100
  let query = {
    practice: row.practice,
  }
  listHearRecordDetail(query).then((res) => {
    if (res.code === 200) {
      console.log(res)
      if (res.data === undefined || res.data === null || res.data.length === 0) {
        proxy.$modal.msgWarning("此记录详情已被删除或未查询到数据")
        return
      }
      testResultWindow.value = true
      testCompleteData.value = res.data
      testTotal.value = res.data.length
    }
  })
}

function initPage() {
  handleRecordData()
}

function resetQuery() {
  queryParams.value.pageNum = 1
  queryParams.value.date = ''
  groupNum.value = null
  queryParams.value.organize = ''
  initPage()
}

function inputKeyUpEvent() {
  if (groupNum.value === '') {
    queryParams.value.organize = ''
    handleRecordData()
    return
  }
  if (!isNumberStr(groupNum.value) || groupNum.value % 1 !== 0) {
    proxy.$modal.msgWarning("组号必须为整数")
    return
  }
  if (groupNum.value > 50 || groupNum.value < 1) {
    proxy.$modal.msgWarning("组号必须在1-50之间")
    return
  }
  handleRecordData()
}

function searchByUnit() {
  handleRecordData()
  // handleQuery()
}

</script>

<style scoped lang="scss">
.header {
  margin-top: 20px;
  margin-left: 20px;
}
</style>
